package com.bilibili.java.bookpage.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.java.bookpage.bean.Commodity;
import com.bilibili.java.bookpage.bean.Indent;
import com.bilibili.java.bookpage.bean.Orders;
import com.bilibili.java.bookpage.service.HomePageService;
import com.bilibili.java.bookpage.service.impl.HomePageServiceImpl;
import com.bilibili.java.books.bean.User;
import com.bilibili.java.books.util.Footet;

public class PageServlet extends HttpServlet{

	HomePageService homePage=new HomePageServiceImpl();
	/**
	 * 修改购物车数量
	 */
	
	public void updateShoppingCartNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String number=req.getParameter("number");
		String id=req.getParameter("id");
		
		homePage.updateShoppingCartNumber(id,number);
		
		
	}
	/**
	 * 新增订单
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String[] id=req.getParameterValues("id");
		
		//调用新增订单的方法
		int orderId=homePage.addOrder(id);
		
		List<Orders> orders=homePage.queryOrder(orderId);
		for (Orders orders2 : orders) {
			System.out.println(orders2);
		}
		
		req.setAttribute("orders", orders);
		
		req.getRequestDispatcher(req.getContextPath()+"/jsp/orderinfo.jsp").forward(req, resp);
		
	}
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		doPost(req,resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
				//获取Url-pattern路径
				String method=req.getServletPath();
				
				//截取路径名
				method=method.substring(1, method.length());
				
				Method m=null;
				
				try {
					
					//获取当前类中名字为 method 的方法
					//getDeclaredMethod(String method)在反射中获取类中和传入的method相同的方法，可以是声明的所有类型的方法
					m=this.getClass().getDeclaredMethod(method, HttpServletRequest.class,HttpServletResponse.class);
					
					//调用获得的方法
					m.invoke(this, req,resp);
					
					
				} catch (Exception e) {
				
					e.printStackTrace();
				} 
		
	}

	/**
	 * 用户登录方法
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void accessHomePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//修改字符集编码
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		//获得请求的页码参数
		int pagination=1;
		try {
			pagination=Integer.parseInt(req.getParameter("pagination"));
			
		}catch (Exception e) {
			
		}
		//获得每页显示的条数
		int record=0;
		try {
		record=Integer.parseInt(req.getParameter("record"));
		}catch (Exception e) {
			
		}
		
		//List<Commodity> list=homePage.getBooksList();
		Footet<Commodity> footet=homePage.getFootet(pagination, record);
		
		User u = (User)(req.getSession().getAttribute("user"));
		
		if(u==null) {
		u=new User();
		}
		req.setAttribute("User", u);
		
		//将获得的数据加入域对象中
		req.setAttribute("footet", footet);
		
		//定义转发请求的地址
		RequestDispatcher dispatcher=req.getRequestDispatcher(req.getContextPath()+"/jsp/bookhomepage.jsp");
		//进行转发
		dispatcher.forward(req, resp);
		
		
	}
	/**
	 * 直接进入首页
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void accessHomePagea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		User u=(User)req.getSession().getAttribute("user");
		if(u!=null) {
		req.getSession().removeAttribute("user");
		}
		
		Cookie[] cookies=req.getCookies();
		
		if(cookies!=null&&cookies.length>0) {
			for (Cookie ck : cookies) {
				
				if(ck.getName().equals("pwdCookie")) {
				ck.setMaxAge(0);
				ck.setPath("/");
				resp.addCookie(ck);
				break;
				}
				
			}
			
			
		}
		
		
		//获得请求的页码参数
		int pagination=1;
		try {
			pagination=Integer.parseInt(req.getParameter("pagination"));
			
		}catch (Exception e) {
			
		}
		//获得每页显示的条数
		int record=0;
		try {
		record=Integer.parseInt(req.getParameter("record"));
		}catch (Exception e) {
			
		}
		//为什么不传这个对象就会报空指针异常？？？？
		User user=new User();
		
		req.setAttribute("User", user);
		Footet<Commodity> footet=homePage.getFootet(pagination, record);
		req.setAttribute("footet", footet);
	
		//定义转发请求的地址
		RequestDispatcher dispatcher=req.getRequestDispatcher(req.getContextPath()+"/jsp/bookhomepage.jsp");
		//进行转发
		dispatcher.forward(req, resp);
		
		
	}
	
	
	
	
	/**
	 * 购物车页面
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shoppingCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//修改字符集编码
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		int  id=Integer.parseInt(req.getParameter("id"));
		Map map=new LinkedHashMap();
		List<Indent> list=homePage.orderList(id);
		for (Indent i : list) {
			Commodity c=homePage.getBook(i.getBookId());
			
			map.put(i, c);
			
			
		}
		
		//将获得的数据加入域对象中
		req.setAttribute("Map", map);
		//定义转发请求的地址
		RequestDispatcher dispatcher=req.getRequestDispatcher("/J54Books/jsp/shoppingcart.jsp");
		//进行转发
		dispatcher.forward(req, resp);
		
		
	}
	/**
	 * 跳转书籍购买页面
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void bookOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//修改字符集编码
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		int  bookId=Integer.parseInt(req.getParameter("bookId"));
		int userId=Integer.parseInt(req.getParameter("userId"));
		
		Commodity c=homePage.getBook(bookId);
		req.setAttribute("userId",userId);
		
		req.setAttribute("Commodity", c);
		
		//定义转发请求的地址
		RequestDispatcher dispatcher=req.getRequestDispatcher("/J54Books/jsp/bookbuy.jsp");
		//进行转发
		dispatcher.forward(req, resp);
		
		
	}
	/**
	 * 添加书籍到购物车
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addBookBuy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		int userId=Integer.parseInt(req.getParameter("userId"));
		int bookId=Integer.parseInt(req.getParameter("bookId"));
		int bookNumber=Integer.parseInt(req.getParameter("bookNumber"));
		
		
		Indent indent=new Indent(userId,bookId,bookNumber);
		Indent cha=homePage.queryShoppingCart(userId,bookId);
	
		Commodity commodity=homePage.getBook(bookId);
		
		if(cha==null){
			
			Boolean b=homePage.addBook(indent);
			
			if(b) {
				resp.getWriter().print("新增购物车成功");
				return;
				
			}
			
			resp.getWriter().print("新增购物车失败");
			
		}else{
		
			if(commodity.getStock()<(bookNumber+cha.getBookNumber())){
				
				resp.getWriter().print("超出库存,新增购物车失败");
				return;
			}
			
			Boolean b1=homePage.updateBook(indent);
			
			if(b1) {
				resp.getWriter().print("新增购物车成功");
				return;
				
			}
			
			resp.getWriter().print("新增购物车失败");
			
			
			
		}
		
		
	}
	/**
	 * 删除指定id 的购物车
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void bookDelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("text/html");
		int id=Integer.parseInt(req.getParameter("id"));
		
		Boolean b1=homePage.delectBook(id);
		
		if(b1) {
			resp.getWriter().print("删除成功");
			return;
			
		}
		
		resp.getWriter().print("删除失败");
		
	}
	
	
	
}
